﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sudoku
{
    class IntermediateTechniques
    {
        public string[,] data;
        public IntermediateTechniques(string[,] val = null)
        {
            val = new string[9, 9]{  {"4", "0", "3", "0", "0", "8", "0", "0", "0"}, 
                                {"9", "2", "0", "0", "0", "0", "1", "0", "6"},
                                {"0", "0", "0", "0", "0", "6", "0", "0", "0"},
                                {"5", "0", "6", "0", "2", "0", "0", "0", "0"},
                                {"0", "4", "0", "3", "0", "1", "0", "2", "0"},
                                {"0", "0", "0", "0", "7", "0", "9", "0", "8"},
                                {"0", "0", "0", "7", "0", "0", "0", "0", "0"},
                                {"3", "0", "5", "0", "0", "0", "0", "1", "2"},
                                {"0", "0", "0", "1", "0", "0", "5", "0", "9"}
                            };
            this.data = val;
        }

        /**
         * FOR TESTING
         * YOU MUST REMOVE THIS FUNCTION BEFORE INCLUDING INTO YOUR MAIN PROJECT
         */

        /*
        public static void Main(string[] args)
        {
            IntermediateTechniques s = new IntermediateTechniques();
            s.data = new string[9, 9]{  {"4", "", "3", "", "", "8", "", "", ""}, 
                                {"9", "2", "", "", "", "", "1", "", "6"},
                                {"", "", "", "", "", "6", "", "", ""},
                                {"5", "", "6", "", "2", "", "", "", ""},
                                {"", "4", "", "3", "", "1", "", "2", ""},
                                {"", "", "", "", "7", "", "9", "", "8"},
                                {"", "", "", "7", "", "", "", "", ""},
                                {"3", "", "5", "", "", "", "", "1", "2"},
                                {"", "", "", "1", "", "", "5", "", "9"}};

            CRME crme = new CRME(s.data);
            s.data = crme.init();
            s.init();

            for (int row = 0; row < 9; row++)
            {
                for (int col = 0; col < 9; col++)
                {
                    Console.Write(s.data[row, col] + " ");
                }
                Console.WriteLine();
            }

        }
         * 
         * */

        //Init
        public void init()
        {
            this.scanLoneRangersInColumn();
            this.scanLoneRangersInRow();
            this.scanLoneRangersInMiniGrid();
        }


        public void scanLoneRangersInRow()
        {
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; (j < 9) && (this.data[i, j].Length > 1); j++)
                {
                    foreach (char c in this.data[i, j])
                    {
                        Boolean flag = true;
                        for (int jo = 0; (jo < 9) && (jo != j); jo++)
                        {
                            if (this.data[i, jo].IndexOf(c) != -1)
                            {
                                flag = false;
                                break;
                            }
                        }
                        if (flag)
                        {
                            this.data[i, j] = c.ToString();
                        }
                    }
                }
            }

        }

        public void scanLoneRangersInColumn()
        {
            for (int j = 0; j < 9; j++)
            {
                for (int i = 0; (i < 9) && (this.data[i, j].Length > 1); i++)
                {
                    foreach (char c in this.data[i, j])
                    {
                        Boolean flag = true;
                        for (int io = 0; (io < 9) && (io != i); io++)
                        {
                            if (this.data[io, j].IndexOf(c) != -1)
                            {
                                flag = false;
                                break;
                            }
                        }
                        if (flag)
                        {
                            this.data[i, j] = c.ToString();
                        }
                    }
                }
            }
        }

        public void scanLoneRangersInMiniGrid()
        {
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    int[] index = this.getIndexMinigrid(i, j);
                    foreach (char c in this.data[i, j])
                    {
                        Boolean flag = true;
                        for (int io = index[0]; io < index[0] + 3; io++)
                        {
                            for (int jo = index[1]; jo < index[1] + 3; jo++)
                            {
                                if ((io != i) && (jo != j))
                                {
                                    if (this.data[io, jo].IndexOf(c) != -1)
                                    {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                            if (!flag) break;
                        }
                        if (flag)
                        {
                            this.data[i, j] = c.ToString();
                        }
                    }
                }
            }
        }

        public int[] getIndexMinigrid(int i, int j)
        {
            int[] index = new int[] { 0, 0 };
            index[0] = ((int)(i / 3)) * 3;
            index[1] = ((int)(j / 3)) * 3;
            return index;
        }
    }
}
